package nl.rainbowsheep.reisorg.empl.site.controller;

import java.util.List;
import javax.validation.Valid;
import nl.rainbowsheep.reisorg.empl.site.model.Country;
import nl.rainbowsheep.reisorg.empl.site.service.CityService;
import nl.rainbowsheep.reisorg.empl.site.service.CountryService;
import nl.rainbowsheep.reisorg.empl.site.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/country")
public class CountryController {

    @Autowired
    CountryService countryservice;

    @Autowired
    RegionService regionservice;

    @Autowired
    CityService cityservice;

    @RequestMapping(value = {"/", "/index", ""})
    public ModelAndView getAll() {
        List<Country> countries = countryservice.getAll();
        ModelAndView model = new ModelAndView("country/index");
        model.addObject("countries", countries);
        return model;
    }

    @RequestMapping(value = {"destination"})
    public List<Country> getAllCountries() {
        List<Country> countries = countryservice.getAll();
        return countries;
    }

    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public ModelAndView getCountry(@PathVariable long id) {
        Country country = countryservice.getById(id);
        ModelAndView mav = new ModelAndView("country/details");
        mav.addObject("country", country);
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView Create() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("country", new Country());
        return mav;
    }

    /**
     *
     * @param country
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String Create(@ModelAttribute("country") Country country, BindingResult bindingResult) {
        country.setDescription("");
        countryservice.insertCountry(country);
        // moet nog
        return "destination";
    }
}
